package realtime.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by luoqifei on 17-5-22.
 */
public class PipelinedDemo {
    private Jedis jedis;
    private  Pipeline pipeline;
    public static int counter = 0;
    private static String fieldSample = "23.7612%s%s,";
    private static long timestamp=System.currentTimeMillis();
    public PipelinedDemo(String ip,int port){
        jedis = new Jedis(ip,port);
        pipeline = jedis.pipelined();
    }
    public static void flushDb(){
        Jedis jedis = new Jedis("127.0.0.1",6379);
        jedis.flushDB();
    }
    private long getTimeMinute(long unixTime){
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
        Date date = new Date(unixTime);
        String dateTime = df.format(date);
        try {
            return df.parse(dateTime).getTime()/1000;
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }
    public void writeRecords(int secs){
        int fgNum = 1;
        int assetNum = 60;
        int fieldNum = 47;
        long seconds = secs;
        long writeSpeed = 200;
        String[] fgIds = new String[fgNum];
        String[] compoundIds = new String[assetNum];
        String[] filedNames = new String[fieldNum];
        String[] filedValues = new String[fieldNum];
        for(int b=0;b<fgNum;b++){
            fgIds[b]="fieldGroupId_"+b;
        }
        for(int b=0;b<assetNum;b++){
            compoundIds[b]="compoundId_"+b;
        }
        for(int i=0;i<fieldNum;i++){
            filedNames[i] = "fieldName_"+i;
            filedValues[i] = String.format(fieldSample,String.valueOf(i+1),String.valueOf(i+1));
        }
        long time=System.currentTimeMillis();
        for(int b=0;b<fgNum;b++){
            for(int k=0;k<assetNum;k++){
                for(int j=0;j<seconds*writeSpeed;j++){
                    setRecordWithPip(timestamp+j*5,filedNames,filedValues,fgIds[b],compoundIds[k]);
                }
            }
        }
        System.out.println("had execute redis write "+counter+" .");
        System.out.println("costs "+(System.currentTimeMillis()-time)/1000+" seconds");
    }
    public boolean setRecordWithPip(long times, String[] fName,String[] fValues,String fgId,String compoundId){
        long time = getTimeMinute(times);
        pipeline.zadd("idx::"+fgId+":"+compoundId+"::"+time,times,""+times);
        counter = counter+1;
        for(int i=0;i<fName.length;i++){
            pipeline.hset("dat::"+compoundId+"::"+fName[i]+"::"+time,fgId+":"+times,fValues[i]);
            pipeline.hincrBy("sts_dat::"+compoundId+"::"+fName[i]+"::"+time,fgId,1);
           // pipeline.hset("cleanlist::rep1","dat::"+compoundId+"::"+fName[i]+"::"+time,time);

            counter = counter+1;
        }

        pipeline.sync();
//        try {
//            pipeline.sync();
//        }catch (Exception e){
//            e.printStackTrace();
//        }finally {
//            if(pipeline !=null){
//                try {
//                    pipeline.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }

        return true;
    }
    public static void main(String args[]){
       flushDb();
        PipelinedDemo PipelinedDemo = new PipelinedDemo("127.0.0.1",6379);
        //PipelinedDemo.writeRecords(1);
    }

}
